<template>
	<view >
		<uni-nav-bar left-icon="left" :statusBar="true"   titleSize="30px" leftText="返回"  rightText="提交"    title="新增维修报单" @clickRight="addFn"  @clickLeft="back"   :fixed="true" backgroundColor="#3286ed" color="#fff"/>
	    
		 <u--form class="form" labelPosition="left" labelWidth="120px" :model="formData" :rules="rules" ref="form">
			 <u-form-item leftIcon="*" leftIconStyle="color:red;"  label="维修编号" prop="repairNo" borderBottom ref="item1">
			  	<u--input :readonly="true" border="none" v-model="formData.repairNo" placeholder="维修编号"></u--input>
			  		<!-- <u-icon @click="show=1" slot="right" name="arrow-right"></u-icon> -->
			  </u-form-item>
			  <u-form-item  leftIcon="*" leftIconStyle="color:red;" label="发起人" prop="applicantName" borderBottom ref="item1">
			   	<u--input   :readonly="true"   border="none" v-model="formData.applicantName" placeholder="发起人" ></u--input>
			    <!-- <u-icon slot="right" size="30" customStyle="padding-right:10px;" @click="codeBtn"	name="scan"></u-icon> -->
			   </u-form-item>
			  <u-form-item  leftIcon="*" leftIconStyle="color:red;" label="维修人员" prop="maintenancePersonnel" borderBottom ref="item1">
			   	<u--input     border="none" v-model="formData.maintenancePersonnel" placeholder="请填写维修人员" ></u--input>
			    <!-- <u-icon slot="right" size="30" customStyle="padding-right:10px;" @click="codeBtn"	name="scan"></u-icon> -->
			   </u-form-item>
				<u-form-item  leftIcon="*" leftIconStyle="color:red;" label="电话" prop="telephone" borderBottom ref="item1">
				 	<u--input     border="none" v-model="formData.telephone" placeholder="请填写电话" ></u--input>
				  <!-- <u-icon slot="right" size="30" customStyle="padding-right:10px;" @click="codeBtn"	name="scan"></u-icon> -->
				 </u-form-item>
				
				 <u-form-item leftIcon="*" leftIconStyle="color:red;"  label="维修描述" prop="repairDescription" borderBottom ref="item1">
				 	<u--textarea v-model="formData.repairDescription" placeholder="维修描述" ></u--textarea>
				  	<!-- <u--input  disabledColor="#fff"  border="none" v-model="formData.repairDescription" placeholder="请输入维修描述"></u--input> -->
				 </u-form-item>
				 <u-form-item leftIcon="*" leftIconStyle="color:red;"  label="维修配件费" prop="partsCost" borderBottom ref="item1">
				  	<u--input  type="number" @change="sumFn" border="none" v-model="formData.partsCost" placeholder="维修配件费"></u--input>
				 </u-form-item>
				 
				<u-form-item leftIcon="*" leftIconStyle="color:red;"  label="维修人工费" prop="laborCost" borderBottom ref="item1">
				 	<u--input type="number" @change="sumFn"  border="none" v-model="formData.laborCost" placeholder="维修人工费"></u--input>
				</u-form-item>
				<u-form-item  leftIcon="*" leftIconStyle="color:red;" label="费用合计" prop="repairAmount" borderBottom ref="item1">
				 	<u--input  :readonly="true"  border="none" v-model="formData.repairAmount" placeholder="费用合计"></u--input>
				</u-form-item>
				<u-form-item leftIcon="*" leftIconStyle="color:red;"  label="租赁订单编号" prop="orderNo" borderBottom ref="item1">
					<u--input  :readonly="true"   border="none" v-model="formData.orderNo" placeholder="请输入租赁销售订单编号"></u--input>
				</u-form-item>
				<u-form-item   leftIcon="*" leftIconStyle="color:red;" label="扫码报修" prop="machineNo" borderBottom ref="item1">
				 	<u--input  disabledColor="#fff"  border="none" v-model="formData.machineNo" placeholder="请输入机器编号" ></u--input>
				  <u-icon slot="right" size="30" customStyle="padding-right:10px;" @click="codeBtn"	name="scan"></u-icon>
				 </u-form-item>
				 <u-form-item   leftIcon="*" leftIconStyle="color:red;" label="机器序列号" prop="machineNo" borderBottom ref="item1">
				  	<u--input  disabledColor="#fff"  border="none" v-model="machineNum" placeholder="请输入机器序列号" ></u--input>
				   
				  </u-form-item>
				  <u-button type="" @click="btSearch" text="搜索"></u-button>
				 <u-form-item   label="机器表"  borderBottom ref="item1">
				  </u-form-item>
				  <view class="">
				  	<uni-table :border="true" stripe emptyText="暂无更多数据">
				  	    <uni-tr>
				  	        <uni-th align="center" width="80">机器序列号</uni-th>
				  	        <uni-th align="center" width="90">产品名称</uni-th>
				  	        <uni-th align="center" width="90">型号</uni-th>
				  	        <uni-th align="center" width="100">仓库</uni-th>
							<uni-th align="center" width="100">操作</uni-th>
				  			  
				  	    </uni-tr>
				  	    <uni-tr v-for="(item,index) in formData.repairMachineList" :key="item.id">
				  	        <uni-td>{{item.machineSerialNumber}}</uni-td>
				  	        <uni-td>{{item.productName}}</uni-td>
				  	        <uni-td>{{item.model}}</uni-td>
				  	        <uni-td>{{item.warehouseName}}</uni-td>
							<uni-td >
								<view class="">
									<button @click="deleteFn(index)" class="uni-button" size="mini" type="warn">删除</button>
								</view>
							</uni-td>
				  	    </uni-tr>
				  	</uni-table>
				  </view>
		 </u--form>
		
		<u-action-sheet :actions="list" title="报修类型" @close="show=false" :show="show" @select="selectFn"></u-action-sheet>
	   
		<u-toast ref="uToast"></u-toast>
		<u-modal showConfirmButton showCancelButton @confirm="BtnModel" @cancel="BtnShow=false" width="250px" :show="BtnShow" title="提示" >
			<view class="slot-content">
				<rich-text nodes="是否确认提交?"></rich-text>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {addRepairApi} from '@/api/repair.js'
	import {machineRecodeList,machineList} from '@/api/machine/machine.js'
	export default{
		data(){
			return{
				formData:{
					repairNo:'',//维修编号
					machineId:'',//机器id
					machineNo:'',//机器编号
					orderNo:'',//订单编号
					repairType:'',//维修类型
					repairTypeName:'',
					repairAmount:'0',//维修金额
					repairDescription:'',
					orderId:'',
					applicantName:'',
					applicantId:'',
					maintenancePersonnel:'',
					telephone:'',
					partsCost:'0',
					laborCost:'0',
					repairMachineList:[],
					
				},
				show:false,
				// inputShow:false,
				BtnShow:false,
				name:'',
				value:'',
				rules:{
					// machineNo:[{required: true,message:'请输入机器编码',trigger: ['blur', 'change']}],
					repairNo:[{required: true,message:'请输入维修编码',trigger: ['blur', 'change']}],
					repairTypeName:[{required: true,message:'请选择维修类型',trigger: ['blur', 'change']}],
					telephone:[{ type: 'string', required: true, message: '请输入联系电话', trigger: ['blur', 'change']},{
					                validator: (rule, value, callback) => {
					                     return uni.$u.test.mobile(value);},
					                        message: '手机号码不正确',
					                        trigger: [ 'blur']
					                    }
					                ],
					maintenancePersonnel:[{required: true,message:'请填写维修人员',trigger: ['blur', 'change']}],
					repairDescription:[{required: true,message:'请填写维修描述',trigger: ['blur', 'change']}],
					partsCost:[{required: true,message:'请填写配件费用',trigger: ['blur', 'change']}],
					laborCost:[{required: true,message:'请填写维修费用',trigger: ['blur', 'change']}],
					repairAmount:[{required: true,message:'请填写费用合计金额',trigger: ['blur', 'change']}],
					orderNo:[{required: true,message:'请输入订单编号',trigger: ['blur', 'change']}],
				},
				list:[
					{name:'大修',value:0},
					{name:'中修',value:1},
					{name:'小修',value:1}
				],
				machineNum:'',
				machines:{}
			}
		},
		onLoad(options){
			console.log(options);
			this.formData.repairNo=this.setOrderNo()
			this.formData.applicantId=this.$store.getters.userid
			this.formData.applicantName=this.$store.getters.name
			if(JSON.stringify(options)!='{}') {
				this.formData.machineNo=JSON.parse(options.item).machineNo
				this.formData.orderId=JSON.parse(options.item).orderId
				this.formData.orderNo=JSON.parse(options.item).orderNo
			}
			
			
		},
		methods:{
			// 费用合计
			sumFn(){
				this.formData.repairAmount=this.formData.partsCost*1+this.formData.laborCost*1
				this.formData.repairAmount=this.formData.repairAmount.toString()
			},
			// 手动搜索机器
			async btSearch(){
				let res=await machineList({machineSerialNumber:this.machineNum})
				 var manchine={
					 machineSerialNumber:res.rows[0].machineSerialNumber,
					 productName:res.rows[0].productName,
					 productNo:res.rows[0].productNo,
					 productId:res.rows[0].productId,
					 machineId:res.rows[0].id,
					 model:res.rows[0].model,
					 machineNo:res.rows[0].machineNo,
					 status:res.rows[0].status,
					 warehouseId:res.rows[0].warehouseId,
					 warehouseName:res.rows[0].warehouseName,
					 image:res.rows[0].image,
					 qrImage:res.rows[0].qrImage,
					 remark:null
					 
				 }
				 this.machines=manchine
				 if(this.formData.repairMachineList.length==0){
					
					  await this.getorderInfo(res.rows[0].machineNo)
					   
				 }else{
					let ismachine= this.formData.repairMachineList.some(item=>
						
						 item.machineSerialNumber==res.rows[0].machineSerialNumber
					 )
					 // 判断是否订单
					 
					 
					  
					if(!ismachine){
						await this.getorderInfo(res.rows[0].machineNo)
						
					}else{
						this.$refs.uToast.show({
							type:"warning",
							message:"重复扫码"
						})
					}
				 }  
				 
				
			},
			// 根据机器获取订单
			getorderInfo(val){
				machineRecodeList({machineNo:val}).then((res)=>{
					if(res.rows[0].orderNo==this.formData.orderNo ||this.formData.orderNo==''){
						this.formData.orderNo=res.rows[0].orderNo
						this.formData.orderId=res.rows[0].orderId
						this.formData.salesmanName=res.rows[0].salesmanName
						this.formData.salesmanId=res.rows[0].salesmanId
						this.formData.repairMachineList.push(this.machines)
					}else{
						this.$refs.uToast.show({
							type:'warning',
							message:"此机器和其他机器不在同一订单"
						})
					}
				})	
				
			},
			deleteFn(index){
				console.log(index);
				 this.formData.repairMachineList.splice(index,1)
			},
			back(){
				uni.navigateBack({
					delta:1
				})
			},
			setOrderNo(){
			      var date = new Date();
			      var year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
			      var month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
			      var day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
			      //修改月份格式
			      if (month >= 1 && month <= 9) {
			        month = "0" + month;
			      }
			      //修改日期格式
			      if (day >= 0 && day <= 9) {
			        day = "0" + day;
			      }
			
			      let round = Math.floor(Math.random() * (999 - 100)) + 100;
			      var currentFormatDate = `${year}${month}${day}`;
			      return "WXD"+currentFormatDate+round;
			    },
				PopBtn(){
					this.show=true
				},
				selectFn(e){
					console.log(e);
					this.formData.repairType=e.value
					this.formData.repairTypeName=e.name
					this.show=false
				},

				
				// 扫码
				 codeBtn(){
					var that =this
					console.log('21313');
					var that =this
					uni.scanCode({
						scanType: ['qrCode'],
						success: function (res) {
							console.log(res);
							console.log(res.result);
							setTimeout(()=>{
								that.formData.machineNo=res.result
								machineRecodeList({machineNo:res.result}).then((res)=>{
									console.log(res);
									that.formData.orderNo=res.rows[0].orderNo
									that.formData.orderId=res.rows[0].orderId
									that.formData.salesmanName=res.rows[0].salesmanName
									that.formData.salesmanId=res.rows[0].salesmanId
								
								})	
								machineList({machineNo:that.formData.machineNo}).then(response=>{
									var manchine={
											 machineSerialNumber:response.rows[0].machineSerialNumber,
											 productName:response.rows[0].productName,
											 productNo:response.rows[0].productNo,
											 productId:response.rows[0].productId,
											 machineId:response.rows[0].id,
											 model:response.rows[0].model,
											 machineNo:response.rows[0].machineNo,
											 status:response.rows[0].status,
											 warehouseId:response.rows[0].warehouseId,
											 warehouseName:response.rows[0].warehouseName,
										     image:response.rows[0].image,
											 qrImage:response.rows[0].qrImage,
											 remark:null
														 
									}
									that.formData.repairMachineList.push(manchine)
								})
							},100)
							
						 
							
						}
					})
				
				},
				// 获取订单编号
				 
				// 提交
				async addFn(){
					this.$refs.form.validate().then(async(res)=>{
						console.log(res);
						if(res){
							this.BtnShow=true
						}
					})
					
				},
				async BtnModel(){
					this.BtnShow=false
					const res= await  addRepairApi(this.formData)
					 if(res.code==200){
						 this.$refs.uToast.show({
							 message:"添加成功！",
							 complete(){
								 uni.navigateBack({
									 delta:1 
								 })
							 }
						 })
					 }
				}
			
		}
	}
</script>

<style lang="scss">
	.form{
		background-color: #fff;
		padding: 0 10px;
	}
	.popup-box{
		min-width: 250px;
		min-height: 100px;
		text-align: center;
		
		.title-top{
			height:40px;
			line-height: 40px;
			font-size: 16px;
			font-weight: 600;
		}
		.box-bottom{
			display: flex;
			justify-content: space-around;
			height:40px;
			line-height: 40px;
			view{
				width:50%;
			}
		}
	}
</style>